package com.sanweibook.jhook.server.facade.sys.impl;

import com.google.common.collect.Lists;
import com.sanweibook.jhook.biz.sys.SysMenuService;
import com.sanweibook.jhook.common.ErrorCodeEnum;
import com.sanweibook.jhook.common.result.Result;
import com.sanweibook.jhook.common.util.JhookObjectUtils;
import com.sanweibook.jhook.dal.model.sys.SysMenu;
import com.sanweibook.jhook.dubbo.dto.sys.SysMenuDTO;
import com.sanweibook.jhook.dubbo.facade.sys.SysMenuFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

/**
 * Created by twg on 2017/5/2.
 */
@Slf4j
@Service("rpcSysMenuFacade")
public class SysMenuFacadeImpl implements SysMenuFacade {
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public Result<SysMenuDTO> getById(Long id) {
        Optional<SysMenu> sysMenu = Optional.ofNullable( sysMenuService.getById( id ) );
        if (sysMenu.isPresent()) {
            return Result.returnSuccessResult( JhookObjectUtils.objectToObject( sysMenu.get(), SysMenuDTO.class ) );
        }
        return Result.returnErrorResult( "", "" );
    }

    @Override
    public Result<SysMenuDTO> getByParentId(Long parentId) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setParentId( parentId );
        Optional<SysMenu> menu = Optional.ofNullable( sysMenuService.get( sysMenu ) );
        return Result.returnSuccessResult( JhookObjectUtils.objectToObject( menu.get(), SysMenuDTO.class ) );
    }

    @Override
    public Result<List<SysMenuDTO>> list(SysMenuDTO sysMenuDTO) {
        SysMenu sysMenu = new SysMenu();
        JhookObjectUtils.copyProperties( sysMenuDTO, sysMenu );
        List<SysMenu> sysMenus = sysMenuService.queryAll( sysMenu );
        return Result.returnSuccessResult( JhookObjectUtils.listToList( sysMenus, SysMenuDTO.class ) );
    }

    @Override
    public Result<List<SysMenuDTO>> listByMenuIds(List<Long> menuIds) {
        if (CollectionUtils.isEmpty( menuIds )) {
            return Result.returnErrorResult( ErrorCodeEnum.PARAMETER_ERROR.getCode(), ErrorCodeEnum.PARAMETER_ERROR.getMessage() );
        }
        List<SysMenu> sysMenus = sysMenuService.queryByIds( menuIds );
        List<SysMenuDTO> sysMenuDTOS = getSysMenus( sysMenus );
        return Result.returnSuccessResult( sysMenuDTOS );
    }

    @Override
    public Result<String> saveAndUpdate(SysMenuDTO sysMenuDTO) {
        SysMenu sysMenu = JhookObjectUtils.objectToObject( sysMenuDTO, SysMenu.class );
        if (sysMenu.getId() == null) {
            sysMenuService.save( sysMenu );
        } else {
            sysMenuService.update( sysMenu );
        }
        return Result.returnSuccessResult( ErrorCodeEnum.SUCCESS.getMessage() );
    }

    private List<SysMenuDTO> getSysMenus(List<SysMenu> sysMenus) {
        List<SysMenuDTO> sysMenuDTOS = Lists.newArrayList();
        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getIsShow() == 1) {
                SysMenuDTO sysMenuDTO = new SysMenuDTO();
                BeanUtils.copyProperties( sysMenu, sysMenuDTO );
                SysMenu menu = new SysMenu();
                menu.setParentId( sysMenu.getId() );
                List<SysMenu> menus = sysMenuService.queryAll( menu );
                sysMenuDTO.setChildren( getSysMenus( menus ) );
                sysMenuDTOS.add( sysMenuDTO );
            }
        }
        return sysMenuDTOS;
    }
}
